// 用户相关API

import api from '../index';
import {
  ApiResponse,
  User,
  PublicUser,
  UpdateUserProfileRequest,
} from '../../types';

// 获取当前用户信息
export const getCurrentUser = async (): Promise<ApiResponse<User>> => {
  const response = await api.get('/users/me');
  return response.data;
};

// 查看指定用户信息
export const getUserById = async (
  userId: number
): Promise<ApiResponse<User | PublicUser>> => {
  const response = await api.get(`/users/${userId}`);
  return response.data;
};

// 更新个人信息
export const updateUserProfile = async (
  data: UpdateUserProfileRequest
): Promise<ApiResponse<User>> => {
  // 如果包含文件上传，使用FormData
  const formData = new FormData();
  
  if (data.username) {
    formData.append('username', data.username);
  }
  
  if (data.bio) {
    formData.append('bio', data.bio);
  }
  
  if (data.avatar) {
    formData.append('avatar', data.avatar);
  }

  const response = await api.put('/users/me', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

 